<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>event-rule-common</title>
    
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/table.jsp"%>
	
	<script type="text/javascript">
	function datagrid(){
		var oTable=	$('#depmtList').dataTable();
		oTable._fnAjaxUpdate();
	}
	$(function(){
		//加载grid
		var oTable=	$('#depmtList').dataTable({
			"bProcessing": false, 
			"bServerSide": true,
			"sServerMethod": 'POST',
			"sAjaxSource":'${pageContext.request.contextPath}/event/event-rule!list.action?resId='+'${resId}',
			"bPaginate": false,
			"bSort": true,
			"bFilter":false,
			"bJQueryUI":false,
			"aoColumns":[
				<%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
				{"mDataProp":"op","":"center","bSortable": false},
				<%}%>
				{"mDataProp":"resName","bSortable": false},
				{"mDataProp":"eventTypeName","bSortable": false},
				{"mDataProp":"levelName","bSortable": false},
				{"mDataProp":"expr","bSortable": false},
				{"mDataProp":"duration","bSortable": false},
				{"mDataProp":"vailTime","bSortable": false},
				{"mDataProp":"status","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
					var imgSrc = oData.statusIndex==1?"collect_Enable.png":"collect_disable.png";
					$(nTd).html("<img width='16px' height='16px'src='../style/default/bsm/images/"+imgSrc+"' title='"+sData+"' />"+sData);
				}},
				{"mDataProp":"id","bVisible":false}
			], 
			 "oLanguage": { 
				 "sZeroRecords": "没有找到记录",
					"sInfo":" ",
					"sInfoEmpty": " "
			}
		});
	});	
	function add(){
		window.location.href="${pageContext.request.contextPath}/event/event-rule!add.action?resId=${resId}&pId=${pId}&viewId=${viewId}";
	}
	function update(id){
		$.ajax({
    		type : "POST",
    		url : "${pageContext.request.contextPath}/event/event-rule!checkDeleted.action", //请求的action
    		data : {id:id}, //传的参数
    		dataType : 'json',
    		success : function(data) { //结果
    			if(data.flag == true){
    				window.location.href="${pageContext.request.contextPath}/event/event-rule!input.action?resId=${resId}&pId=${pId}&viewId=${viewId}&id="+id;
    			}else{
    				getTopWin(window).getMessageBox().alert("此规则已经被删除了","提示");
    				datagrid();
    			}
    		}
    	});
	}
	function view(id,flag){
		//自定义
		if(flag == '1'){
			$.ajax({
	    		type : "POST",
	    		url : "${pageContext.request.contextPath}/event/event-rule!checkDeleted.action", //请求的action
	    		data : "id="+id, //传的参数
	    		dataType : 'json',
	    		success : function(data) { //结果
	    			if(data.flag == true){
	    				var url ="iframe:event/event-rule!view.action?resId="+'${resId}'+"&id="+id+"&flag="+flag;
	    				var opt = { title: "查看事件规则",width:650,height:530,buttons: {}};
	    				getTopWin(window).openjBox(url,opt,'view-event-rule',window);
	    			}else{
	    				getTopWin(window).getMessageBox().alert("此规则已经被删除了","提示");
	    				datagrid();
	    			}
	    		}
	    	});
		}else{
			var url ="iframe:event/event-rule!view.action?resId="+'${resId}'+"&id="+id+"&flag="+flag;
			var opt = { title: "查看事件规则",width:650,height:530,buttons: {}};
			getTopWin(window).openjBox(url,opt,'view-event-rule',window);
		}
	}
	function stop(id,flag,resId){
		//自定义
		if(flag == '1'){
			var submit = function (v, h, f) {
			    if (v == 'ok') {
			    	$.ajax({
			    		type : "POST",
			    		url : "${pageContext.request.contextPath}/event/event-rule!stop.action", //请求的action
			    		data : "id="+id+"&flag="+flag+"&resId="+resId, //传的参数
			    		dataType : 'json',
			    		success : function(data) { //结果
			    			window.setTimeout(function () { $.jBox.tip('禁用成功!', 'success'); }, 100);
		    				datagrid();
			    		}
			    	});
			    }
			    else if (v == 'cancel') {
			    }
			    return true; 
			};
			getTopWin(window).jBox.confirm("确定要禁用此条自定义事件规则吗？", "提示", submit);
		}else{
			var submit = function (v, h, f) {
			    if (v == 'ok') {
			    	$.ajax({
			    		type : "POST",
			    		url : "${pageContext.request.contextPath}/event/event-rule!stop.action", //请求的action
			    		data : "id="+id+"&flag="+flag+"&resId="+resId, //传的参数
			    		dataType : 'json',
			    		success : function(data) { //结果
			    			window.setTimeout(function () { $.jBox.tip('禁用成功!', 'success'); }, 100);
		    				datagrid();
			    		}
			    	});
			    }
			    else if (v == 'cancel') {
			    }
			    return true; 
			};
			getTopWin(window).jBox.confirm("确定要禁用此条默认事件规则吗？", "提示", submit);
		}
	}
	function start(id,flag,resId){
		//自定义
		if(flag == '1'){
			var submit = function (v, h, f) {
			    if (v == 'ok') {
			    	$.ajax({
			    		type : "POST",
			    		url : "${pageContext.request.contextPath}/event/event-rule!start.action", //请求的action
			    		data : "id="+id+"&flag="+flag+"&resId="+resId, //传的参数
			    		dataType : 'json',
			    		success : function(data) { //结果
			    			window.setTimeout(function () { $.jBox.tip('启用成功!', 'success'); }, 100);
		    				datagrid();
			    		}
			    	});
			    }
			    else if (v == 'cancel') {
			    }
			    return true; 
			};
			getTopWin(window).jBox.confirm("确定要启用此条自定义事件规则吗？如果有事件级别和事件类别都相同的默认规则存在，则将被禁用", "提示", submit);
		}else{
				var submit = function (v, h, f) {
				    if (v == 'ok') {
				    	$.ajax({
				    		type : "POST",
				    		url : "${pageContext.request.contextPath}/event/event-rule!start.action", //请求的action
				    		data : "id="+id+"&flag="+flag+"&resId="+resId, //传的参数
				    		dataType : 'json',
				    		success : function(data) { //结果
				    				window.setTimeout(function () { $.jBox.tip('启用成功!', 'success'); }, 100);
				    				datagrid();
				    		}
				    	});
				    }
				    else if (v == 'cancel') {
				    }
				    return true; 
				};
				getTopWin(window).jBox.confirm("确定要启用此条默认事件规则吗？如果有事件级别和事件类别都相同的自定义事件规则存在，则将被禁用", "提示", submit);
		}
	}
	function del(id){
		var submit = function (v, h, f) {
		    if (v == 'ok') {
		    	$.ajax({
		    		type : "POST",
		    		url : "${pageContext.request.contextPath}/event/event-rule!delete.action", //请求的action
		    		data : {id:id}, //传的参数
		    		dataType : 'json',
		    		success : function(data) { //结果
		    			if(data == true){
		    				window.setTimeout(function () { getTopWin(window).jBox.tip('删除成功。', 'success'); }, 100);
							datagrid();
		    			}else{
		    				getTopWin(window).getMessageBox().alert("此规则已经被删除了","提示");
		    				datagrid();
		    			}
		    		}
		    	});
		    }
		    else if (v == 'cancel') {
		    }
		    return true; //close
		};
		getTopWin(window).jBox.confirm("确定要删除数据吗？", "提示", submit);
	}
	function applyRes(resIds){
		getTopWin(window).getMessageBox().tip("执行中...", 'loading');
		$.ajax({
				type:'post',
				url:'${pageContext.request.contextPath}/event/event-rule!batchApplySave.action?ids='+resIds+'&resId=${resId}',
				global:false,
				async:false,
				cache:false,
				dataType:'json',
				success:function(data,textStatus){
					window.setTimeout(function () {
						getTopWin(window).getMessageBox().tip('复制到同类资源成功。', 'success'); 
					},1500);
				},
				error:function(){
					getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
				}
		});
	}
	function applyWindow(){
		var url ='iframe:event/event-rule!apply.action?resId=${resId}';
	    var opt =  {title:'复制到同类资源',id:'applyResWindow',width:710,height:440,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
	    getTopWin(window).openjBox(url,opt,'event-rule-apply',window);
	}
	</script>
  </head>
  
  <body>
  	<h2 class="title-h2"><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>事件规则结果列表</h2>
  	<div>
  	  	<%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
  		<div class="toolbar" id = "toolbar">
	 		<a class="page_add" href="javascript:add();">新增</a>
	 		<a class="page_edit" href="javascript:applyWindow();">复制到同类资源</a>
		</div>
		<%}%>
	    <div>
			<table id="depmtList" width="100%" cellpadding="0" cellspacing="0" border="0" class="display">
				<thead>
					<tr>
						<%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
							<th width="3%">操作</th>
						<%}%>	
						<th width="12%">资源名称</th>
						<th width="15%">事件类别</th>
						<th width="8%">事件级别</th>
						<th width="30%">事件表达式</th>
						<th width="5%">持续时间(秒)</th>
						<th width="10%">有效时间段</th>
						<th width="8%">状态</th>
					</tr>
				</thead>
			</table>
		</div>
  	</div>
  </body>
</html>